﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DAO;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class DAO_QueQuan
    {
        private DataServices data = new DataServices();
        public DataTable getAllQueQuan() 
        {
            try 
	        {	        
		        String sql = "SELECT * FROM " + DTO_QueQuan.TB_Name;
                return data.executeGetDataTable(sql);
	        }
	        catch (Exception ex)
	        {
		        throw new Exception(ex.Message,ex);
	        }
        }

        public bool insertQueQuan(DTO_QueQuan queQuan)
        {
            bool result = false;
            try
            {
                String sql = "INSERT INTO " + DTO_QueQuan.TB_Name
                                    + " ( " + DTO_QueQuan.TENQUEQUAN + " ) VALUES (@" + DTO_QueQuan.TENQUEQUAN + " ) ";
                SqlParameter p1 = new SqlParameter(DTO_QueQuan.TENQUEQUAN, queQuan.TenQueQuan);
                result = data.executeUpdate(sql, p1);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message,ex);
            }
            
            return result;
        }

        public bool updateQueQuan(DTO_QueQuan queQuan)
        {
            bool result = false;
            try
            {
                String sql = "UPDATE " + DTO_QueQuan.TB_Name
                                    + " SET " + DTO_QueQuan.TENQUEQUAN + " =@" + DTO_QueQuan.TENQUEQUAN 
                                    + " WHERE " + DTO_QueQuan.MAQUEQUAN + " =@" + DTO_QueQuan.MAQUEQUAN;
                SqlParameter p1 = new SqlParameter(DTO_QueQuan.TENQUEQUAN, queQuan.TenQueQuan);
                SqlParameter p2 = new SqlParameter(DTO_QueQuan.MAQUEQUAN, queQuan.MaQueQuan);
                result = data.executeUpdate(sql, p1,p2);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }

            return result;
        }
        public bool deleteQueQuan(DTO_QueQuan queQuan)
        {
            bool result = false;
            try
            {
                String sql = "deleteQueQuan";
                SqlParameter p1 = new SqlParameter(DTO_QueQuan.MAQUEQUAN, queQuan.MaQueQuan);
                result = data.updateFormProcedure(sql, p1);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }

            return result;
        }

        public DataTable getQueQuanByName(string name)
        {
            try
            {
                String sql = "SELECT * FROM " + DTO_QueQuan.TB_Name + " WHERE "
                                    + DTO_QueQuan.TENQUEQUAN + " LIKE @" + DTO_QueQuan.TENQUEQUAN;
                SqlParameter p1 = new SqlParameter(DTO_QueQuan.TENQUEQUAN, "%" + name + "%");
                return data.executeGetDataTable(sql, p1);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
    }
}
